package com.dream.wukong.configcenter.client.core.storage;


import com.dream.wukong.configcenter.client.core.IDataSource;
import com.dream.wukong.configcenter.client.core.ProxyProperty;
import com.dream.wukong.configcenter.client.remote.http.HttpDataSource;
import com.netflix.config.DynamicStringProperty;
import org.apache.log4j.Logger;

/**
* 描  述: 动态配置服务类
* 创建时间: 2018/5/22
*/
public class DynamicConfig {

    private static Logger logger = Logger.getLogger(DynamicConfig.class);

    private static IDataSource dataSource = HttpDataSource.getInstance();;

    public static IDataSource getDataSource() {
        return dataSource;
    }

    public static ProxyProperty getDataSource(String sourceName) {
        return dataSource.getConfiguration(sourceName);
    }


    /**
    * 描  述:  添加callback函数,当DynamicStringProperty的值发生改变时对本地内存中的值进行更新
    * 参  数:dynamicStringProperty
    * 返回值: void
    * 创建时间: 2018/5/22
    */
    public static void addCallBack(DynamicStringProperty dynamicStringProperty) {
        logger.info( dynamicStringProperty.getName() +"添加回调");
        dynamicStringProperty.addCallback(() -> {
            dataSource.setConfiguration(dynamicStringProperty.getName(),dynamicStringProperty.getValue());
        });

    }

}
